import { getHouselist } from '../api/house.ts';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

const IndexModel = {
  namespace: 'house',

  state: {
    houselist: [],
    // searchlist:[]
  },

  effects: {
    *GET_HOUSE({ payload }, { call, put }) {
      const data = yield getHouselist({ payload });
      // console.log(data);
      yield put({ type: 'SET_HOUSE', payload: data.data });
    },
  },
  reducers: {
    SET_HOUSE(state, { payload }) {
      state.houselist = payload;
      return {
        ...state,
      };
    },
    ADD_HOUSES(state, { payload }) {
      const { title, id } = payload;
      // console.log(payload)
      const newState = JSON.parse(JSON.stringify(state));
      // console.log(newState)
      const index = newState.houselist.findIndex((item) => {
        return item.id === id;
      });
      console.log(index);
      newState.houselist[index].title = title;
      console.log(newState);
      return {
        ...newState,
      };
    },
    GET_SEARCH(state, payload) {
      // console.log(payload.value);
      const searchlist = state.houselist.filter((item) => {
        return item.title.includes(payload.value);
      });
      return {
        ...state,
        houselist: searchlist,
      };
    },
    DEL_DATA(state, { payload }) {
      const newState = JSON.parse(JSON.stringify(state));
      newState.houselist.splice(payload, 1);
      // console.log(state)
      // const index=  state.houselist.findIndex(item=>item.id === payload.id)
      // state.houselist.splice(index,1)
      return {
        ...newState,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    AAA({ dispatch }) {
      dispatch({ type: 'GET_HOUSE' });
    },
  },
};

export default IndexModel;
